
// 获取应用实例
const app = getApp()


Page({
  data: {
    baseURL:wx.baseURL,
    newsTimer:0,
    newsIndex:0,
    banners:[],
    news:[],
    Icon:[],
    timer:0,
    lists: [],
    newItems:'transform: translate(0rpx,0rpx);transition:transform 0.3s;',
    pageNum:1,
    total:0,
    loading:'数据加载中。。。。'

  },
  // 新闻
//获得新闻轮播数据
getNews(){
  return new Promise((resolve,reject)=>{
    wx.$https.get('/index/news').then((res)=>{
        console.log('打印新闻',res)
      this.setData({
        news:res
      })
      this.changeNewsIndex()
      resolve()
    })
  })
},
changeNewsIndex(){
  if(this.data.newsTimer){
    clearInterval(this.data.newsTimer)
    this.data.newsTimer=0
  }
  this.data.newsTimer=setInterval(()=>{
    let newsIndex=this.data.newsIndex
    newsIndex ++
    // if(newsIndex == this.data.news.length) newsIndex=0
    this.setData({
      newsIndex,
      newItems:'transform: translate(0rpx,-'+newsIndex * 80 + 'rpx);transition:transform 1s;'
    })
    if(newsIndex == 4){
      setTimeout(()=>{
        this.setData({
          newsIndex:0,
          newItems:'transform:translate(0rpx,0rpx)'
        })
      },500)
    }
  },2000)
},


//下拉刷新
onPullDownRefresh(){
  this.data.pageNum=1
  this.data.recommending=[]
  Promise.all([this.getNews(),this.getBanners(),this.getIcon(),this.getRecomend()]).then(() =>{
    console.log('所有数据加载完成')
   }).catch(e=>{
    wx.stopPullDownRefresh()//异常停止刷新
   })
  
},
// 轮播图
getBanners(){
  return new Promise((resolve,reject)=>{
    wx.get('/index/banners').then((res)=>{
      this.setData({
        banners:res
      })
      resolve()
    })



  })
  
},
// 轮播图跳转事件
swiperClick(){
 
  let path=  this.data.banners.path
  wx.switchTab({
    url: this.path,
  })

},
// 品牌
getIcon(){
  return new Promise((resolve,reject)=>{
   
    wx.get('/index/categorys').then(res=>{
       this.setData({
          Icon:res
       })
       reject(222)
    })


  //   wx.request({
  //   url: 'https://obuge.com/juzao/index/categorys',
  //   success: (res) =>{
  //     console.log(res.data)
  //     this.setData({
       
  //     })
  //     resolve()
  //   }
  // })
  })
  
},
// 推荐
onReachBottom(){
  if(this.data.total>this.data.lists.length){
    this.data.pageNum ++
    this.getRecomend()
  }
},
//获取推荐分页数据
getRecomend(){
  return new Promise((resolve,reject)=>{
    this.setData({loading:'数据加载中...'})
   wx.get('/index/recommends?pageNum=' + this.data.pageNum).then(res=>{
     this.setData({
                 loading:'小主，已经到底了噢~',
        lists:this.data.lists.concat(res.rows),
        total:res.total
     })
     reject(111)
   })

  // wx.request({
  //   url: 'https://obuge.com/juzao/index/recommends?pageNum=' + this.data.pageNum,

  //   success: (res) =>{
  //     console.log('打印推荐列表',res.data)
  //     this.setData({
  //       loading:'已加载完成',
  //       lists:this.data.lists.concat(res.data.rows),
  //       total:res.data.total
  //     })
  //     resolve()
  //   }
  // })

  })
  
},




onLoad(){

  // 封装get this.getNews().then(()=>{
  //   return this.getNews()
  // })
   Promise.all([this.getNews(),this.getBanners(),this.getIcon(),this.getRecomend()]).then(() =>{
    console.log('所有数据加载完成')
   }).catch(e=>{
    console.log(e)
   })
  this.changeNewsIndex()
  this.onReachBottom()
},


})